package com.ming.sps.server.services;

import java.io.FileInputStream;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.ming.sps.server.bean.ParamMap;
import com.ming.sps.server.utils.DateTimeUtil;
import com.ming.sps.server.utils.SqlUtil;

import ming.util.base.Const;

public class TerminalService extends BaseService {
	
	public ParamMap getMachine(String terminalName) throws Exception{
		ParamMap terminal = dao.queryOneBySql("select t.*,a.code as orgcode,a.name as orgname from t_tobacco_terminal t left join t_basedata_area a on a.id=t.area where vcName = ?",SqlUtil.constructParam(terminalName));
		return terminal;
	}
	
	public int updateMachineByName(Map<String,Object> map) throws Exception{
		return dao.update("t_tobacco_terminal", "vcname", map);
	}
	
	
	public List<ParamMap> getModule(String terminalName) throws Exception{
		List<ParamMap> loginType = dao.queryListBySql("select m.* from t_tobacco_module m left join t_module_terminal t on m.id=t.module_id where t.terminal_id='" + terminalName + "' or m.MODULE_ID is not null order by seq");
		return loginType;
	}
	
	
	public List<ParamMap> getLicense(String terminalName) throws Exception{
		List<ParamMap> loginType = dao.queryListBySql("select b.* from t_tobacco_license_terminal a left join t_bussiness_license b on a.vcPrintType_ID=b.ID where  vcTerminal_ID =(select vcid from t_tobacco_terminal where vcname=?) order by b.sort", SqlUtil.constructParam(terminalName));
		return loginType;
	}
	
	
	public List<ParamMap> getPublish(String terminalName) throws Exception{
		List<ParamMap> publish = dao.queryListBySql("select vcid,intpublish,dtpublishtime,vctitle,vctype from t_tobacco_publish where vcid in (select vcPublish_ID from t_tobacco_publish_terminal where vcTerminal_ID in (select vcid from t_tobacco_terminal where vcName=?)) order by dtPublishTime desc", SqlUtil.constructParam(terminalName));
		return publish;
	}
	public ParamMap getPublishDetail(String id) throws Exception{
		ParamMap publish = dao.queryOneBySql("select * from t_tobacco_publish where vcid=? ", SqlUtil.constructParam(id));
		String vccontent = publish.getString("vccontent");
		if(vccontent != null) {
			vccontent = vccontent.replaceAll("(\r\n|\n|\n\r)", "");
			publish.put("vccontent", vccontent);
		}
		return publish;
	}
	public ParamMap getPublishDetailByParam(String type,String mname) throws Exception{
		ParamMap publish = dao.queryOneBySql("select * from t_tobacco_publish where vcType=? and vcid in (select vcPublish_ID from t_tobacco_publish_terminal where vcTerminal_ID in (select vcid from t_tobacco_terminal where vcName=?)) order by dtPublishTime desc", SqlUtil.constructParam(type,mname));
		return publish;
	}
	
	public List<ParamMap> getPrinttypeUpdate(String terminalName) throws Exception{
		List<ParamMap> update = dao.queryListBySql("select * from Z_TM_PRINTTYPE_UPDATE where terminal = ? and state=?", SqlUtil.constructParam(terminalName,0));
		return update;
	}
	
	
	public boolean auth(String mname,String token) throws Exception{//update TBAUTH set state='1' where username='T0031';
		boolean flag = false;
		if(Const.map.containsKey(token)){
			flag = true;
		}else{
			boolean isExist = false;
			List<ParamMap> auths = dao.queryListBySql("select * from TBAUTH");
			for (ParamMap paramMap : auths) {
				if(mname.equals(paramMap.getString("username"))){
					if(token.equals(paramMap.getString("code"))){
						isExist =  true;
						if("1".equals(paramMap.getString("state"))){
							Const.map.put(token, token);
							flag =  true;
						}
					}
				}
				
			}
			if(!isExist){
				Map<String,Object> map  = new LinkedHashMap<String, Object>();
				map.put("createtime", DateTimeUtil.getDateTime());
				map.put("username", mname);
				map.put("code", token);
				map.put("state", "0");
				dao.insert("TBAUTH", map, "id");
			}
		}
		return flag;
		
	}

	public static void main(String[] args) throws Exception {
		FileInputStream fin = new FileInputStream("d:/1.txt");
		byte [] b = new byte[102400];
		int length = fin.read(b);
		String str = new String(b, 0, length);
		str = str.replaceAll("(\r\n|\n|\n\r)", "");
		System.out.println(str);
		fin.close();
	}

}
